<script setup lang="ts">
import { Delete } from "@element-plus/icons-vue";
import { deleteCar } from "~/api/goods";
import { useImageCache } from "~/composables/useImageCache";
import defaultAvatar from "@/assets/app-mj.png";

// 使用图片缓存功能
const { getCoverImage, preloadImages } = useImageCache();

const router = useRouter();

const props = defineProps({
  dataItem: {
    type: Object,
    default: () => ({}),
  },
});
const emits = defineEmits(["reloadList"]);

// 处理头像显示
const avatarSrc = computed(() => {
  if (props.dataItem.shopAvatar) {
    return getCoverImage(props.dataItem.shopAvatar);
  }
  return defaultAvatar;
});

const delCat = async () => {
  const goodsId = props.dataItem.goodsId;
  const data = await deleteCar({
    goodsId,
  });
  emits("reloadList");
};

const toGoodsDetails = () => {
  router.push({
    path: "/details",
    query: {
      id: props.dataItem.goodsId,
    },
  });
};
</script>

<template>
  <div class="cart-item" @click="toGoodsDetails">
    <div class="goods-img"><img :src="getCoverImage(props.dataItem.coverPic)" alt="" /></div>
    <div class="goods-info">
      <h4 class="name">{{ props.dataItem.goodsName }}</h4>
      <p class="info">
        <span>{{ props.dataItem.addCarCount }}人加入购物车</span>
        <span v-if="props.dataItem.favorCommentCount">|</span>
        <span v-if="props.dataItem.favorCommentCount"
          >{{ props.dataItem.favorCommentCount || 0 }}条好评</span
        >
      </p>
      <div class="user">
        <div class="user-info">
          <span class="avatar">
            <el-avatar :size="20" :src="avatarSrc" :alt="props.dataItem.shopName || '用户头像'">
              <template #error>
                <img :src="defaultAvatar" :alt="props.dataItem.shopName || '用户头像'" style="width: 100%; height: 100%; object-fit: cover;" />
              </template>
            </el-avatar>
          </span>
          <span class="user-name">{{ props.dataItem.shopName }}</span>
          <span class="lv">Lv.{{ props.dataItem.level || 1 }}</span>
        </div>
        <span class="price"><span>¥</span>{{ props.dataItem.goodsAmount || '面议' }}</span>
      </div>
    </div>
    <span class="del" @click.stop="delCat"
      ><el-icon class="icon"><Delete /></el-icon> 移除</span
    >
  </div>
</template>

<style scoped lang="scss">
.cart-item {
  display: flex;
  align-items: center;
  background: #ffffff;
  border-radius: 16px 16px 16px 16px;
  border: 1px solid rgba(25, 30, 40, 0.08);
  padding: 24px;
  margin-bottom: 16px;
  cursor: pointer;
}
.goods-img {
  width: 200px;
  height: 130px;
  overflow: hidden;
  border-radius: 12px;
  margin-right: 16px;
  img {
    width: 100%;
    height: 100%;
  }
}
.goods-info {
  flex: 1;
  .name {
    font-size: 20px;
    color: #191e28;
    line-height: 36px;
  }
  .info {
    font-size: 12px;
    color: #1dbf73;
    display: flex;
    align-items: center;
    margin: 10px 0;
    span:nth-child(2) {
      padding: 6px;
    }
    span:nth-child(3) {
      color: rgba(25, 30, 40, 0.36);
    }
  }
  .user {
    display: flex;
    align-items: center;
    .user-info {
      flex: 1;
      display: flex;
      align-items: center;
      .avatar {
        width: 20px;
        height: 20px;
        margin-right: 6px;
      }
      .user-name {
        color: rgba(25, 30, 40, 0.72);
        padding-right: 4px;
      }
      .lv {
        font-weight: bold;
        color: #008571;
        font-size: 12px;
      }
    }
    .price {
      font-size: 24px;
      color: #ff4f4f;
      font-weight: bold;
      span {
        font-size: 16px;
      }
    }
  }
}
.del {
  align-self: flex-start;
  flex: 0 0 80px;
  margin-left: 100px;
  line-height: 36px;
  color: #191e28;
  cursor: pointer;
  .icon {
    vertical-align: -2px;
  }
  &:hover {
    color: #ff4f4f;
    font-weight: bold;
  }
}
</style>
